﻿using RS.Win32API;
using RS.Win32API.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;

namespace ZuesControls.Controls
{
    public class ZSWindowbase:Window
    {



        public ZSWindowbase()
        {
            #region 这里样式必须在构造函数配置
            this.WindowStyle = WindowStyle.None;
            this.AllowsTransparency = true;
            #endregion
            this.StateChanged += RSWindow_StateChanged;
            this.Loaded += RSWindowBase_Loaded;
            this.Closing += RSWindow_Closing;
        }


        public HwndSource HwndSource;

        public static readonly DependencyProperty IsMaxsizedFullScreenProperty =
          DependencyProperty.Register("IsMaxsizedFullScreen", typeof(bool), typeof(ZSWindow), new PropertyMetadata(false));

        [Description("窗口最大化时是否全屏")]
        [Browsable(false)]
        public bool IsMaxsizedFullScreen
        {
            get { return (bool)GetValue(IsMaxsizedFullScreenProperty); }
            set { SetValue(IsMaxsizedFullScreenProperty, value); }
        }
        private void RSWindow_Closing(object? sender, CancelEventArgs e)
        {
            
        }

        private void RSWindowBase_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.RefreshWindowSizeAndLocation();
            }
        }

        private void RSWindow_StateChanged(object? sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.RefreshWindowSizeAndLocation();
            }
        }

        private void RefreshWindowSizeAndLocation()
        {
            // 使用 WindowInteropHelper 获取窗口句柄
            var hWnd = new WindowInteropHelper(this).Handle;
            int nWidth = IsMaxsizedFullScreen ? (int)SystemParameters.PrimaryScreenWidth : (int)SystemParameters.WorkArea.Width;  // 新的宽度
            int nHeight = IsMaxsizedFullScreen ? (int)SystemParameters.PrimaryScreenHeight : (int)SystemParameters.WorkArea.Height; // 新的高度
            NativeMethods.SetWindowPos(new HandleRef(null, hWnd), new HandleRef(null, IntPtr.Zero), 0, 0, nWidth, nHeight, (int)(SWP.NOZORDER | SWP.NOACTIVATE));
        }
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            this.HwndSource = (HwndSource)PresentationSource.FromVisual(this);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }

    }
}
